package org.feng.pojo;

import lombok.Data;
import org.feng.enums.Response;

/**
 * 公共返回对象
 *
 * @author feng
 */
@Data
public class Result<T> {
    private Boolean flag;
    private Integer code;
    private String msg;
    private T data;

    public static <T> Result<T> status(boolean bool) {
        return bool ? success() : error();
    }

    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(true, 200, msg, data);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(true, 200, "操作成功", data);
    }

    public static <T> Result<T> success() {
        return new Result<>(true, 200, "操作成功");
    }

    public static <T> Result<T> error(String msg) {
        return new Result<>(true, 500, msg);
    }

    public static <T> Result<T> error() {
        return new Result<>(false, 500, "操作失败");
    }

    public static <T> Result<T> error(Response response) {
        return new Result<>(false, response.getCode(), response.getMsg());
    }

    public static <T> Result<T> error(Integer code, String msg) {
        return new Result<>(false, code, msg);
    }

    public Result(Boolean flag, Integer code, String msg) {
        this.flag = flag;
        this.code = code;
        this.msg = msg;
    }

    public Result(Boolean flag, Integer code) {
        this.flag = flag;
        this.code = code;
    }

    public Result(Boolean flag) {
        this.flag = flag;
    }

    public Result(Boolean flag, Integer code, String msg, T data) {
        this.flag = flag;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
}
